#include "screenlook.h"
#include <QPainter>
#include <QDebug>
#include <QThread>
#include <QImage>
ScreenLook::ScreenLook(DeviceInfo *deviceInfo,QWidget *parent) : VPxDecoder(parent)
{
    setMouseTracking(true);
    this->qtyInfo = deviceInfo;
    init(deviceInfo);
}
ScreenLook::~ScreenLook(){
    // qInfo("析构 ScreenLook::~ScreenLook");
}
void ScreenLook::init(DeviceInfo *deviceInfo){
    QThread *thread = new QThread;
    activeHandler  = new ActiveHandler(this) ;
    activeHandler->initInfo(deviceInfo,NetWorkHandler::ACTIVE);

    //线程开始,网络处理类创建socket
    connect(thread, &QThread::started, activeHandler, &ActiveHandler::createSocket);
    //关闭信号,网络类关闭socket
    connect(this, &ScreenLook::closeSignal, activeHandler, &ActiveHandler::removeSocket);
    //    //transfer结束了，其他关联也结束
    connect(activeHandler, &ActiveHandler::finished, this, &ScreenLook::finishedSockeHandler);
    connect(activeHandler, &ActiveHandler::finished, thread, &QThread::quit);
    //线程结束,网络类销毁
    connect(thread, &QThread::finished, activeHandler, &ActiveHandler::deleteLater);
    //线程结束,线程实例销毁
    connect(thread, &QThread::finished, thread, &QThread::deleteLater);
    //暂时没作用
    connect(activeHandler, &ActiveHandler::connectedStatus,this, &ScreenLook::connStatusHandler);
    connect(activeHandler, &ActiveHandler::remoteConnectError,this, &ScreenLook::showNotErrorDlg);
    connect(activeHandler, &ActiveHandler::showInputPasswdDlg,this, &ScreenLook::showInputPasswdDlgHandler);
    //错误对话框关闭时，发送信号
    connect(&dlg_remoteErrror,SIGNAL(dlg_ok()),this,SIGNAL(closeSignal()));
    //密码对话框  到  请求认证
    connect(&dlg_inputpasswd,SIGNAL(connectRemoteDevice(QString)),this,SLOT(passConnect(QString)));
    connect(this, &ScreenLook::requestAuth, activeHandler, &ActiveHandler::slotRequestAuth);
    connect(activeHandler, &ActiveHandler::imageParameters,this, &ScreenLook::setPicSize);
    //控制事件
    connect(this,SIGNAL(mouseMoveSend(QPoint)),activeHandler,SLOT(slotSendMouseMove(QPoint)));
    connect(this,SIGNAL(sendMouseKeys(int,bool)),activeHandler,SLOT(slotSendMouseKeys(int,bool)));
    connect(this,SIGNAL(sendWheelEvent(bool)),activeHandler,SLOT(slotSendWheelEvent(bool)));
    connect(this,SIGNAL(sendKeyboard(int,bool)),activeHandler,SLOT(slotSendKeyboard(int,bool)));
    //开始连接
    activeHandler->moveToThread(thread);
    //开始线程，触发createSocket
    thread->start();
}

//密码连接
void ScreenLook::passConnect(QString password){
    emit requestAuth(qtyInfo->getRemoteDeviceID()
                     ,password);
}
//显示错误会话框
void ScreenLook::showNotErrorDlg(QString tips){
    dlg_remoteErrror.setErrorTips(tips);
    dlg_remoteErrror.exec();
}
//显示密码输入框
void ScreenLook::showInputPasswdDlgHandler(){
    dlg_inputpasswd.exec();
}

void ScreenLook::connStatusHandler(bool conn){
    //重连
}
void ScreenLook::finishedSockeHandler(){
    activeHandler = Q_NULLPTR;
    this->deleteLater();
}
void ScreenLook::setPicSize(int width,int height){
    this->m_imageWidth = width;
    this->m_imageHeight = height ;
    lastMap = QPixmap(QSize(width,height));
    this->show();
}
void ScreenLook::closeEvent(QCloseEvent *event) {
    emit closeSignal();
    //接受退出信号，程序退出
    event->accept();
}
void ScreenLook::mouseMoveEvent(QMouseEvent *event){
    //当前窗体坐标
    QPoint currentPoint = event->pos();
    //当前窗体大小
    QSize currentSize = this->size()  ;
    int really_x = currentPoint.x() * m_imageWidth / currentSize.width() ;
    int really_y = currentPoint.y() * m_imageHeight / currentSize.height();
    QPoint newPoint(really_x,really_y);
    emit mouseMoveSend(newPoint);
}
void ScreenLook::keyReleaseEvent (QKeyEvent* event)
{
    emit sendKeyboard(event->nativeVirtualKey(),false);
}
//键盘事件
void ScreenLook::keyPressEvent(QKeyEvent * event)
{
    emit sendKeyboard(event->nativeVirtualKey(),true);
}

void ScreenLook::mousePressEvent(QMouseEvent *event)
{
    state = true ;
    if(event->button() == Qt::LeftButton)
    {
        code = 0 ;
    }
    else if(event->button() == Qt::MidButton){
        code = 1 ;
    }
    else if(event->button() == Qt::RightButton)
    {
        code = 2 ;
    }
    emit sendMouseKeys(code,state) ;
}
void ScreenLook::mouseReleaseEvent(QMouseEvent *event)
{
    state = false ;
    if(event->button() == Qt::LeftButton)
    {
        code = 0 ;
    }
    else if(event->button() == Qt::MidButton){
        code = 1 ;
    }
    else if(event->button() == Qt::RightButton)
    {
        code = 2 ;
    }
    emit sendMouseKeys(code,state) ;
}
void ScreenLook::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        emit sendMouseKeys(0,true) ;
    }

}

void ScreenLook::wheelEvent(QWheelEvent *event)
{

    if(event->delta() > 0)// 当滚轮远离使用者时
    {
        emit sendWheelEvent(false);
    }
    else//当滚轮向使用者方向旋转时
    {
        emit sendWheelEvent(true);
    }
}


